package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysDictType;
import com.ruoyi.system.domain.dto.SysDictTypeDTO;
import com.ruoyi.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Objects;

@RestController
@RequestMapping("/admin/dict/type")
@Api(tags = "(运营端) 数据字典")
public class SysDictTypeController {
    @Resource
    private ISysDictTypeService dictTypeService;

    @GetMapping("/list")
    @ApiOperation(value = "字典列表查询",response = SysDictType.class)
    public ResultEntity list(SysDictTypeDTO params)
    {
        PageUtil.startPage(params);
        return ResultEntity.createSuccess(PageUtil.getPageList(dictTypeService.getDictTypeList(params)));
    }

    /**
     * 查询字典类型详细
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据id查询字典信息",response = SysDictType.class)
    public ResultEntity getInfo(@ApiParam(value = "字典id", required = true) @PathVariable("id")String id)
    {
        return ResultEntity.createSuccess(dictTypeService.getById(id));
    }

    /**
     * 新增字典类型
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增字典",response = SysDictType.class)
    public ResultEntity add(@RequestBody SysDictTypeDTO params)
    {
        SysDictType dictType = dictTypeService.getOne(new LambdaQueryWrapper<SysDictType>().eq(SysDictType :: getDictType,params.getDictType()));
        if(Objects.nonNull(dictType)){
            throw new RuntimeException("新增字典失败，字典类型已存在!");
        }
        SysDictType entity = new SysDictType();
        BeanUtils.copyProperties(params,entity);
        entity.setCreateBy(SecurityUtils.getLoginUser().getUser().getId());
        entity.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        dictTypeService.save(entity);
        return ResultEntity.createSuccess(entity);
    }

    /**
     * 修改字典类型
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改字典",response = SysDictType.class)
    public ResultEntity edit(@RequestBody SysDictTypeDTO params)
    {
        SysDictType dictType = dictTypeService.getById(params.getId());
        if(Objects.isNull(dictType)){
            throw new RuntimeException("字典不存在!");
        }
        BeanUtils.copyProperties(params,dictType);
        dictType.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        dictTypeService.saveOrUpdate(dictType);
        return ResultEntity.createSuccess(dictType);
    }

    /**
     * 删除字典类型
     */
    @PostMapping("/{dictIds}")
    @ApiOperation(value = "删除字典-批量",response = SysDictType.class)
    public ResultEntity remove(@PathVariable String[] dictIds)
    {
        return ResultEntity.createSuccess(dictTypeService.removeByIds(Arrays.asList(dictIds)));
    }

}
